package com.mdd.admin.controller.subject;

import com.mdd.admin.config.aop.Log;
import com.mdd.admin.service.subject.ISubjectCourseService;
import com.mdd.admin.service.subject.ISubjectService;
import com.mdd.admin.validate.subject.SubjectCourseBatchParam;
import com.mdd.admin.validate.subject.SubjectCourseParam;
import com.mdd.admin.validate.subject.SubjectParam;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.vo.course.CourseListVo;
import com.mdd.admin.vo.subject.SubjectListVo;
import com.mdd.admin.vo.subject.SubjectDetailVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 专题专区管理
 * @author panweiliang
 */
@RestController
@RequestMapping("api/subject")
public class SubjectController {

    @Resource
    ISubjectService iSubjectService;

    @Resource
    ISubjectCourseService iSubjectCourseService;

    /**
     * 专题专区列表
     *
     * @author panweiliang
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return Object
     */
    @GetMapping("/list")
    public Object list(@Validated PageParam pageParam,
                       @RequestParam Map<String, String> params) {
        PageResult<SubjectListVo> list = iSubjectService.list(pageParam, params);
        return AjaxResult.success(list);
    }

    /**
     * 专题专区详情
     *
     * @author panweiliang
     * @param id 主键ID
     * @return Object
     */
    @GetMapping("/detail")
    public Object detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        SubjectDetailVo detail = iSubjectService.detail(id);
        return AjaxResult.success(detail);
    }

    /**
     * 专题专区新增
     *
     * @author panweiliang
     * @param subjectParam 参数
     * @return Object
     */
    @Log(title = "专题专区新增")
    @PostMapping("/add")
    public Object add(@Validated(value = SubjectParam.create.class) @RequestBody SubjectParam subjectParam) {
        iSubjectService.add(subjectParam);
        return AjaxResult.success();
    }

    /**
     * 专题专区编辑
     *
     * @author panweiliang
     * @param subjectParam 参数
     * @return Object
     */
    @Log(title = "专题专区编辑")
    @PostMapping("/edit")
    public Object edit(@Validated(value = SubjectParam.update.class) @RequestBody SubjectParam subjectParam) {
        iSubjectService.edit(subjectParam);
        return AjaxResult.success();
    }

    /**
     * 专题专区删除
     *
     * @author panweiliang
     * @param subjectParam 参数
     * @return Object
     */
    @Log(title = "专题专区删除")
    @PostMapping("/del")
    public Object del(@Validated(value = SubjectParam.delete.class) @RequestBody SubjectParam subjectParam) {
        iSubjectService.del(subjectParam.getId());
        return AjaxResult.success();
    }

    /**
     * 专题专区状态
     *
     * @author panweiliang
     * @param subjectParam 课程参数
     * @return AjaxResult
     */
    @Log(title = "专题专区状态更改")
    @PostMapping("/change")
    public AjaxResult change(@Validated(value = SubjectParam.change.class)
                             @RequestBody SubjectParam subjectParam) {
        return AjaxResult.success(iSubjectService.changeStatus(subjectParam.getId()));
    }

    /**
     * 专题/专区-课程列表
     *
     * @author panweiliang
     * @param subjectId 专题/专区id
     * @return Object
     */
    @GetMapping("/course/list")
    public Object list(@RequestParam(value = "subjectId") Integer subjectId) {
        List<CourseListVo> list = iSubjectService.listCourse(subjectId);
        return AjaxResult.success(list);
    }

    /**
     * 专题专区移除课程
     *
     * @author panweiliang
     * @param subjectCourseParam 参数
     * @return Object
     */
    @Log(title = "专题专区移除课程")
    @PostMapping("/course/del")
    public Object del(@Validated(value = SubjectCourseParam.delete.class) @RequestBody SubjectCourseParam subjectCourseParam) {
        iSubjectCourseService.del(subjectCourseParam.getCourseId(), subjectCourseParam.getSubjectId());
        return AjaxResult.success();
    }

    /**
     * 专题专区新增课程
     *
     * @author panweiliang
     * @param subjectCourseParam 参数
     * @return Object
     */
    @Log(title = "专题专区新增课程")
    @PostMapping("/course/add")
    public Object addCourse(@Validated(value = SubjectCourseParam.create.class) @RequestBody SubjectCourseParam subjectCourseParam) {
        iSubjectCourseService.add(subjectCourseParam);
        return AjaxResult.success();
    }

    /**
     * 专题专区新增课程
     *
     * @author panweiliang
     * @param subjectCourseBatchParam 参数
     * @return Object
     */
    @Log(title = "专题专区新增课程")
    @PostMapping("/course/addInBulk")
    public Object batch_addCourse(@Validated(value = SubjectCourseBatchParam.create.class) @RequestBody SubjectCourseBatchParam subjectCourseBatchParam) {
        iSubjectCourseService.addInBulk(subjectCourseBatchParam);
        return AjaxResult.success();
    }

}
